﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using Conusic;
using Conusic.Properties;

namespace Imager
{
    public partial class ImageEditor
    {
        private void ImageEditorMouseMove(object sender, MouseEventArgs e)
        {
            if (dragComponent != null)
            {
                if (dragtype == DragTypes.move)
                {
                    dragComponent.Move(e.X - dragStartPointInFigure.X, e.Y - dragStartPointInFigure.Y);
                }
                else if (dragtype == DragTypes.resize)
                {
                    dragComponent.Move(e.X, e.Y);
                }
            }
            else
            {
                var figure = GetComponentInPosition(e.X, e.Y);
                if (figure == null)
                {
                    Cursor = Cursors.Arrow;
                }
                else if (figure is Marker)
                {
                    switch ((figure as Marker).markerType)
                    {
                        case Marker.MarkersType.TopLeft:
                            Cursor = Cursors.PanNW;
                            break;
                        case Marker.MarkersType.TopRight:
                            Cursor = Cursors.PanNE;
                            break;
                        case Marker.MarkersType.BottomLeft:
                            Cursor = Cursors.PanSW;
                            break;
                        case Marker.MarkersType.BottomRight:
                            Cursor = Cursors.PanSE;
                            break;
                        default:
                            throw new Exception("Invalid value for MarkersType");
                    }
                }
                else
                {
                    Cursor = Cursors.Hand;
                }
            }

            this.Invalidate();
        }

        private void ImageEditorMouseDown(object sender, MouseEventArgs e)
        {
            dragComponent = GetComponentInPosition(e.X, e.Y);

            if (dragComponent is Marker)
            {
                dragtype = DragTypes.resize;
            }
            else
            {
                selectedComponent = dragComponent;

                if (drawingComponents.Remove(selectedComponent))
                    drawingComponents.Insert(0, selectedComponent);

                dragtype = DragTypes.move;
                if (dragComponent != null)
                {
                    dragStartPointInFigure = new Point(e.X - dragComponent.border.X, e.Y - dragComponent.border.Y);
                }
            }

            if (markers != null)
            {
                markers.Clear();
            }
            else
            {
                markers = new List<Marker>();
            }
            if (selectedComponent != null && selectedComponent.Resizeble)
            {
                markers.Add(new Marker(selectedComponent, Marker.MarkersType.TopLeft));
                markers.Add(new Marker(selectedComponent, Marker.MarkersType.TopRight));
                markers.Add(new Marker(selectedComponent, Marker.MarkersType.BottomLeft));
                markers.Add(new Marker(selectedComponent, Marker.MarkersType.BottomRight));
            }

            if (selectedComponent is TextComponent)
            {
                SetUserFont((selectedComponent as TextComponent).Font);
            }

            this.Invalidate();
        }

        private void ImageEditorMouseUp(object sender, MouseEventArgs e)
        {
            dragComponent = null;
            this.Invalidate();
        }

        private void ImageEditorDragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
            string file = files[0];

            var point = new Point(e.X, e.Y);
            ImportImage(file, point);

            this.Invalidate();
        }


        private void ImportImage(string file, Point point)
        {
            if (!String.IsNullOrEmpty(file))
            {
                try
                {
                    Image img = new Bitmap(file);
                    // Prevent using images internal thumbnail
                    img.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                    img.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

                    if (img.Width > this.Width)
                    {
                        var img2 = img.GetThumbnailImage(this.Width, img.Height*this.Width/img.Width, null, IntPtr.Zero);
                        img.Dispose();
                        img = img2;
                    }

                    if (drawingComponents.Count == 0)
                    {
                        Rectangle graffiti = GetGraffityPart();
                        drawingComponents.Insert(0, new ImageComponent(img, graffityPart.X, graffityPart.Y, 586, 293));
                    }
                    else
                    {
                        drawingComponents.Insert(0,
                                                 new ImageComponent(img, this.PointToClient(point).X,
                                                                    this.PointToClient(point).Y, img.Width, img.Height));
                    }
                }
                catch (Exception ea)
                {
                    throw ea;
                }
            }
        }

        private void ImageEditorDragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void ImageEditorKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                drawingComponents.Remove(selectedComponent);
                selectedComponent = null;
                markers.Clear();
            }
            this.Invalidate();
        }

        private void ImageEditorSizeChanged(object sender, EventArgs e)
        {
            Rectangle re = GetGraffityPart(CurrentSize);
            Rectangle rn = GetGraffityPart();
            //move all componetns to center;
            foreach (var a in drawingComponents)
            {
                a.border.X += rn.X - re.X;
                a.border.Y += rn.Y - re.Y;
            }
            CurrentSize = this.Size;
        }

        private void ImageEditorDoubleClick(object sender, EventArgs e)
        {
            if (selectedComponent is TextComponent)
            {
                (selectedComponent as TextComponent).StartEditText();
            }
            else if (selectedComponent == null)
            {
                var dialog = new OpenFileDialog();
                dialog.Multiselect = false;
                dialog.InitialDirectory = Settings.Default.DefaultDialogDir;
                dialog.Filter =
                    @"Изображения (*.bmp;*.jpg;*.gif;*.png)|*.bmp;*.jpg;*.gif;*.jpeg;*.png|Все файлы (*.*)|*.*";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    Settings.Default.DefaultDialogDir =  Path.GetDirectoryName(dialog.FileName);
                    ImportImage(dialog.FileName, (e as MouseEventArgs).Location);
                }
            }
        }
    }
}